cap program drop get_mean_median_for_fisher_test
program get_mean_median_for_fisher_test
	version 16.1
	args bidder_auction quantilex
/*********************** compute statistics by group *********************/
	keep if fy_con >= 2014 & fy_con <= . & FLAG == 0
	qui gen condition_score = ""
	/* conditioning */
	************************************************
	*local bidder_auction = "auction"
	*local bidder_auction = "bidder"
	************************************************
	foreach period in "after" "before" {
		local st_fy = 2011
		local en_fy = .
		di "****** Before/After: `period' ******"
		if "`period'" == "before"{
			local st_mdybid = "td(1apr2015)"
			local ed_mdybid = "td(12feb2019)"
		}
		else if "`period'" == "after"{
			local st_mdybid = "td(15mar2019)"
			local ed_mdybid = "td(31mar2021)"
		}
		forvalues x = 1(1)28{
			if `x' != 21 & `x' != 24{
				local y = strofreal(`x', "%02.0f")
				di "$$$$$$ Mean(`bidder_auction') group: `y'; Period: `period' $$$$$$$"
				cap drop thebidder
				cap drop thebidder_auctionTG
				local condition_plus = "MDYbid >= `st_mdybid' & MDYbid <= `ed_mdybid' "
				qui gen thebidder = 1 if g28_plus == `x' & /*18aug22->*/ firm_treatment < . & `condition_plus'
				cap drop theauction
				qui bys id: egen theauction = max( thebidder == 1 )
				if "`quantilex'" != ""{
					qui sum percent_reserve if the`bidder_auction' == 1, d
					qui replace the`bidder_auction' = 0 if percent_reserve < `r(`quantilex')'
					local _quantile = "_`quantilex'"
				}
				*qui egen thebidder_auctionTG = tag(id thebidder) if thebidder == 1 /* added on 04sep22*/
				qui count if the`bidder_auction' == 1
				if r(N) > 0{
					qui replace condition_score = "`period'_group:`y'" if thebidder == 1
		
					foreach v in $vars {
						qui /*noisily*/ sum `v' if the`bidder_auction' == 1, d
						if r(N) > 0{
							cap gen Mean_`v' = .
							qui replace Mean_`v' = `r(mean)' if thebidder==1
							*cap gen Median_`v' = .
							*qui replace Median_`v' = `r(p50)' if thebidder==1
							cap gen N_`v' = .
							qui replace N_`v' = `r(N)' if thebidder==1							
						}
					}
				}
			}
		}
	}
	*local bidder_auction = "auction"
	keep g28_plus pair28 condition_* Mean* N* fy_con FLAG
	keep if fy_con >= 2015 & fy_con <= . & FLAG == 0
	keep if condition_score != ""
	egen condition_score_TG = tag(condition_score)
	keep if condition_score_TG == 1
	drop condition_score_TG
	sort condition_score
	drop fy_con FLAG
	save ../fishertest/mean_`bidder_auction'`_quantile'.dta, replace

end